package com.shouer.doc.common.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by ZXL on 2016/11/12.
 */
public class Tools {

    public static String DEFAULT_UUID = null;

    //获取UUID
    public static String getUUID(Context context) {

        if (TextUtils.isEmpty(DEFAULT_UUID)) {
            DEFAULT_UUID = (String) SPUtils.get(context, "default_uuid", "");

            if (TextUtils.isEmpty(DEFAULT_UUID)) {
                String m_szImei = getIMEI(context);
                String m_szDevIDShort = m_szDevIDShort();
                String m_szAndroidID = getAndroidID(context);
                String m_szWLANMAC = getWlanMac(context);

                m_szImei = TextUtils.isEmpty(m_szImei) ? "" : m_szImei;
                m_szDevIDShort = TextUtils.isEmpty(m_szDevIDShort) ? "" : m_szDevIDShort;
                m_szAndroidID = TextUtils.isEmpty(m_szAndroidID) ? "" : m_szAndroidID;
                m_szWLANMAC = TextUtils.isEmpty(m_szWLANMAC) ? "" : m_szWLANMAC;

                StringBuffer m_szLongID = new StringBuffer(m_szImei).append(m_szDevIDShort).append(m_szAndroidID).append(m_szWLANMAC);

                // compute md5
                MessageDigest m = null;
                try {
                    m = MessageDigest.getInstance("MD5");
                } catch (NoSuchAlgorithmException e) {
                    Log.e(Tools.class.getSimpleName(),"getUUID:"+e.toString());
                }
                m.update(m_szLongID.toString().getBytes(), 0, m_szLongID.length());// get md5 bytes
                byte p_md5Data[] = m.digest();
                // create a hex string
                String m_szUniqueID = new String();
                for (int i = 0; i < p_md5Data.length; i++) {
                    int b = (0xFF & p_md5Data[i]);
                    // if it is a single digit, make sure it have 0 in front (proper padding)
                    if (b <= 0xF)
                        m_szUniqueID += "0";
                    // add number to string
                    m_szUniqueID += Integer.toHexString(b);
                }   // hex string to uppercase

                m_szUniqueID = m_szUniqueID.toUpperCase();

                UUID uuid = new UUID(m_szUniqueID.hashCode(), m_szDevIDShort.hashCode() << 32 | m_szWLANMAC.hashCode());

                DEFAULT_UUID = uuid.toString();
                SPUtils.put(context, "default_uuid", DEFAULT_UUID);

            }
        }

        return DEFAULT_UUID;
    }

    /**
     * 获得当期指定的间隔符号,如:20130412100711.
     */
    public static String getTimestamp() {
        String formatString = "yyyyMMddHHmmss";
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        return format.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 获取手机的IMEI码
     */
    public static String getIMEI(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//系统版本在6.0以及6.0以上对APP权限进行判断
            if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
                return "";
        }
        TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String szImei = TelephonyMgr.getDeviceId();
        return szImei;
    }

    /**
     * 获取手机的无线MAC地址
     **/
    public static String getWlanMac(Context context) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
        return m_szWLANMAC;
    }

    public static String getAndroidID(Context context) {
        String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

        return m_szAndroidID;
    }

    public static String m_szDevIDShort() {
        String m_szDevIDShort = "35" + //we make this look like a valid IMEI

                Build.BOARD.length() % 10 +
                Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 +
                Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 +
                Build.HOST.length() % 10 +
                Build.ID.length() % 10 +
                Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 +
                Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 +
                Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 digits

        return m_szDevIDShort;
    }

}
